package com.newtouch.bxzs.database.synchronize.batch.reader;

import lombok.Data;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.NonTransientResourceException;
import org.springframework.batch.item.ParseException;
import org.springframework.batch.item.UnexpectedInputException;

import java.util.List;

/**
 * 读取同步数据
 * <p>
 * <b>History:</b>
 * <table border="1">
 * <tr>
 * <th>Date</th>
 * <th>Operator</th>
 * <th>Memo</th>
 * </tr>
 * <tr>
 * <td>2021/2/25 9:38</td>
 * <td>yuanjie</td>
 * <td>Create</td>
 * </tr>
 * </table>
 *
 * @author yuanjie
 * @version 1.0.0
 * @since 1.0.0
 */
@Data
public class ItemReaderFactory<T> implements ItemReader<T> {

    private Integer index = 0;

    private List<T> list;

    public ItemReaderFactory(List<T> list){
        this.list = list;
    }

    @Override
    public T read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
        T model = null;
        if (index < list.size()) {
            model = list.get(index);
            index++;
        }
        return model;
    }

}
